#include "test.h"


TgoUIButton*	btn = NULL;
TgoUIListBox*	lb	= NULL;
namespace App{

        TestApp testApp;

		Tgo2DEntity *entity = new Tgo2DEntity( D3DXVECTOR3( 320, 240, 0 ), 0.0f );

        void OnInit()
        {
                testApp.OnInit();
        }

        void OnUpdate( float dt )
        {
                testApp.OnUpdate( dt );
        }

        void OnMainLoop()
        {
                testApp.OnMainLoop();
        }

        void OnRelease()
        {
                testApp.OnRelease();
        }


        TestApp::TestApp()
        {

        }

	void TestApp::OnInit()
	{
		mSpriteMgr.OnInit();
		TGO::GetTgoUI().OnInitUI();
		TGO::BasicCamera().UpdateCamera();
		TGO::UIFont().OnInit( L"Tahoma", 7, 16 );

        void TestApp::OnInit()
        {
                mSpriteMgr.OnInit();
                TGO::GetTgoUI().OnInitUI();
                TGO::BasicCamera().UpdateCamera();
                TGO::UIFont().OnInit( L"Tahoma", 15, 30 );

                //ortho projection matrix
                D3DXMATRIX P;
                D3DXMatrixOrthoLH( &P, float(screen_width), float(screen_height), 1.0f, 5000.0f );
                RenderFrame::GetD3DDevice()->SetTransform(D3DTS_PROJECTION, &P);

                //set texture filter
                
                HR( RenderFrame::GetD3DDevice()->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ) );
                HR( RenderFrame::GetD3DDevice()->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ) );
                HR( RenderFrame::GetD3DDevice()->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ) );

                HR( RenderFrame::GetD3DDevice()->SetTextureStageState(
                        0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2) );

                CreateEntities();

        }

        void TestApp::CreateEntities()
        {
#pragma region load_texture
		TextureMgr::Instance()->CreateTextureEX( L"e1.png", "e1" );
		TextureMgr::Instance()->CreateTextureEX( L"testButton.jpg", "testButton" );
		TextureMgr::Instance()->CreateTextureEX( L"testSelector.png", "testSelector" );
		TextureMgr::Instance()->CreateTextureEX( L"testSelector2.png", "testSelector2" );
#pragma endregion

#pragma region create_ui
		
		TgoUIButton* btn1 = new TgoUIButton(  D3DXVECTOR2( 100, 100 ), 128, 64, true );
		btn1->SetSelectTexture( "testSelector" );
		btn1->SetTexture( "testButton" );
		btn1->SetText( L"button1" );

                TgoUIButton* btn2 = new TgoUIButton( D3DXVECTOR2( 100, 200 ), 128, 32, true );
                btn2->SetSelectTexture( "quitSelector" );
                btn2->SetTexture( "quitButton" );
                //btn2->SetText( L"quit game" );


		TGO::GetTgoUI().AddUI( btn1 );
		TGO::GetTgoUI().AddUI( btn2 );

		btn = btn1;
		
		TgoUIListBox* lb1 = new TgoUIListBox( D3DXVECTOR2( 400, 100 ), 128, 64, true );
		lb1->SetSelectorTexture( "testSelector2" );

		lb1->AddItem( L"item1", "testButton" );
		lb1->AddItem( L"item2", "testButton" );
		lb1->AddItem( L"item3", "testButton" );
		lb1->AddItem( L"item4", "testButton" );

		lb = lb1;

		TGO::GetTgoUI().AddUI( lb1 );
		

#pragma endregion


#pragma region creat_2d_entity
		/*
		Tgo2DEntity *entity = new Tgo2DEntity( D3DXVECTOR3( 320, 240, 0 ), 0.0f );
		entity->SetTexture( TextureMgr::Instance()->GetTexture( "e1" ) );
		entity->SetCenter( D3DXVECTOR3( entity->GetWidth() * 0.5f, entity->GetHeight() * 0.5f, 0 ) );

		mSpriteMgr.AddSprite( entity );
		*/
#pragma endregion 
		
	}

        void TestApp::OnMainLoop()
        {
                mSpriteMgr.Render();
                TGO::GetTgoUI().RenderUI();

	void TestApp::OnMainLoop()
	{
		mSpriteMgr.Render();
		TGO::GetTgoUI().RenderUI();

		if ( btn->IsBtnDown() ) {
			MessageBox( 0, L"yep", L"btn is clicked?", 0 );
		}

		if ( lb->GetFocusItem() == 2 ) {
			MessageBox( 0, L"yep", L"item2 is clicked?", 0 );
		}

#pragma region font_test
                RECT rect;
                rect.top = 150;
                rect.left = 150;
                rect.right = 300;
                rect.bottom = 300;
                TGO::UIFont().DrawTextShadowed( L"t2go", rect, 0xFFFFFFFF );
#pragma endregion
                
        }

        void TestApp::OnRelease()
        {
                TGO::GetTgoUI().OnReleaseUI();
                TGO::UIFont().OnRelease();
                mSpriteMgr.ReleaseSprites();
                TextureMgr::Instance()->ReleaseTextures();
        }

}
